package com.renwen.controller;

import com.renwen.domain.News;
import com.renwen.domain.Vo;
import com.renwen.mapper.NewsMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Controller()
@RequestMapping("news")
public class SelectController {
    @RequestMapping(value = "/select", method = RequestMethod.POST)
    public void save(HttpServletResponse response,Vo vo) throws IOException {//不常用
        response.setCharacterEncoding("GBK");
        InputStream is = Resources.getResourceAsStream("com/sqlMapConfig.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = factory.openSession();
        NewsMapper mapper = sqlSession.getMapper(NewsMapper.class);
//        String newsTitle = request.getParameter("newsTitle");
        News news = new News();
        news.setNewsTitle(vo.getNewsTitle());
        System.out.println(news.getNewsTitle());
        List<News> newsList = mapper.selectNewsById(news);
        for (News news1:newsList) {
            response.getWriter().println(news1);
        }
    }
}
